package com.top.ruizhong.main.access

import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.module.lemlin.base.BaseAbstractFragment
import com.module.lemlin.mode.ViewModeFragment
import com.top.ruizhong.R
import com.top.ruizhong.mode.vm.AccessViewModel
import kotlinx.android.synthetic.main.fragment_access_login.*

class LoginFragment : ViewModeFragment<AccessViewModel>() {

    companion object {
        fun newInstance(): LoginFragment {
            return LoginFragment()
        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_login

    override fun viewModel(): AccessViewModel {
        return createViewModel(requireActivity())
    }

    override fun initView() {

        val agrees = "我已阅读并同意瑞众《用户协议》及《隐私政策》"
        val spannable = SpannableString(agrees)
        spannable.setSpan(
            object : ClickableSpan() {
                override fun onClick(widget: View) {

                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = ColorUtils.getColor(R.color.colorMain)
                    ds.isUnderlineText = false
                }
            },
            agrees.indexOf(string = "《"),
            agrees.indexOf(string = "》") + 1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        spannable.setSpan(
            object : ClickableSpan() {
                override fun onClick(widget: View) {

                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = ColorUtils.getColor(R.color.colorMain)
                    ds.isUnderlineText = false
                }
            },
            agrees.lastIndexOf(string = "《"),
            agrees.lastIndexOf(string = "》") + 1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        tvLoginAgree.text = spannable
        tvLoginAgree.movementMethod = LinkMovementMethod.getInstance()

        viewModel.observableEditText(etLoginMobileContent)
    }

    override fun initViewMode() {
        viewModel.contentMobileLoginLiveData.observe(viewLifecycleOwner, Observer { mobile ->
            tvLoginGetCode.isEnabled = !(mobile.isNullOrEmpty() || mobile.length != 11)
        })
    }

}